package com.wanxi.service.impl;

import com.wanxi.dao.CuisineDao;
import com.wanxi.dao.impl.CuisineDaoImpl;
import com.wanxi.dto.CuisineDTO;
import com.wanxi.dto.ResultDTO;
import com.wanxi.model.CuisineModel;
import com.wanxi.service.CuisineService;
import com.wanxi.util.JSONCopy;
import com.wanxi.vo.CuisineVO;

import java.util.List;

public class CuisineServiceImpl implements CuisineService {
    private CuisineDao cuisineDao = new CuisineDaoImpl();

    @Override
    public ResultDTO add(CuisineVO cuisineVO) {
//        CuisineModel cuisineModel=new CuisineModel();
//        cuisineModel.setName(cuisineVO.getName());
        CuisineModel cuisineModel = JSONCopy.copy(cuisineVO, CuisineModel.class);
//        有没有更好的方式取复制对象里面的值
        return ResultDTO.builder()
                .count(cuisineDao.add(cuisineModel))
                .build();
    }

    @Override
    public ResultDTO update(CuisineVO cuisineVO) {
        CuisineModel cuisineModel = JSONCopy.copy(cuisineVO, CuisineModel.class);
        return ResultDTO.builder()
                .count(cuisineDao.update(cuisineModel))
                .build();
    }

    @Override
    public ResultDTO delete(CuisineVO cuisineVO) {
        CuisineModel cuisineModel = JSONCopy.copy(cuisineVO, CuisineModel.class);
        return ResultDTO.builder()
                .count(cuisineDao.delete(cuisineModel))
                .build();
    }

    @Override
    public ResultDTO findById(CuisineVO cuisineVO) {
//        将vo的值复制到model
        CuisineModel cuisineModel = JSONCopy.copy(cuisineVO, CuisineModel.class);
//        将model的值复制到dto
        CuisineDTO cuisineDTO = JSONCopy.copy(cuisineDao.findById(cuisineModel), CuisineDTO.class);
        return ResultDTO.builder()
                .data(cuisineDTO)
                .build();
    }

    @Override
    public ResultDTO count(CuisineVO cuisineVO) {
//        将vo的值复制到model
        CuisineModel cuisineModel = JSONCopy.copy(cuisineVO, CuisineModel.class);
//        将model的值复制到dto
        CuisineDTO cuisineDTO = JSONCopy.copy(cuisineDao.count(cuisineModel), CuisineDTO.class);
        return ResultDTO.builder()
                .data(cuisineDTO)
                .build();
    }

    @Override
    public ResultDTO findAll(CuisineVO cuisineVO) {
        //        将vo的值复制到model
        CuisineModel cuisineModel = JSONCopy.copy(cuisineVO, CuisineModel.class);
//        将model的值复制到dto
        List<CuisineDTO> cuisineDTO = JSONCopy.copy(cuisineDao.findAll(cuisineModel), CuisineDTO.class);
        return ResultDTO.builder()
                .count(cuisineDao.count(cuisineModel))
                .data(cuisineDTO)
                .build();
    }
}
